package com.tg.app.activity.device;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import com.appbase.custom.constant.CommonConstants;
import com.hjq.permissions.Permission;
import com.demo.view.TimeRuleView;
import com.tange.base.toolkit.DateUtil;
import com.tange.base.toolkit.StringUtils;
import com.tange.core.media.source.impl.Speed;
import com.tange.core.media.source.impl.local.DeviceStoragePlayback;
import com.tange.core.universal.instructions.StorageInstruction;
import com.tg.app.R;
import com.tg.app.activity.base.CameraBaseActivity;
import com.tg.app.activity.device.ui.cameraview.PlayBackBaseFragment;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.helper.CameraBottomSheetDialogHelper;
import com.tg.app.widget.BottomSheetListDialog;
import com.tg.appcommon.android.ImageUtils;
import com.tg.appcommon.android.PermissionUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.data.http.entity.WheelData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class PlaybackActivity extends CameraBaseActivity {
    protected final List<DeviceStoragePlayback.Index> list = new ArrayList<>();
    protected final List<TimeRuleView.TimePart> mTimePartList = new ArrayList<>();
    protected final List<TimeRuleView.TimePart> recordTimes = new ArrayList<>();
    protected DeviceStoragePlayback deviceStoragePlayback;
    protected  StorageInstruction.SdCardState sdCardState;
    protected PlayBackBaseFragment mPlayBackFragment;
    protected final int currentResolution = AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN;
    protected WheelData selectDay;
    protected boolean isLandscape = false;

    protected void initPlayback() {
        TGLog.d(TAG, "mPlaybackDisplay = " + mDeviceUIHelper.getPlaybackDisplay());
        mPlayBackFragment = ActivityHelper.newInstance(CommonConstants.CAMERA_TYPE_PLAYBACK_SDCARD, mDevice, false);
        mPlayBackFragment.setDeviceItem(mDevice);
        getGetEnv();

        mPlayBackFragment.setDeviceUIHelper(mDeviceUIHelper);
        if (deviceSettingsInfo != null) {
            TGLog.d(TAG, "deviceSettingsInfo.total = " + deviceSettingsInfo.total);
            if (deviceSettingsInfo.total != -999) {
                mPlayBackFragment.setSdCardSize(deviceSettingsInfo.total, false);
            }
        }
    }

    protected void play(long start){

    }
    protected void play(DeviceStoragePlayback.Index index){
        if (index != null){
            TGLog.d(TAG, "index.start:" + index.getStart() + ",index.end:" + index.getEnd());
            TGLog.d(TAG, "index.start:" + DateUtil.formatTimeHHmmss(mPlayBackFragment.getSecondDay(index.getStart())) + ",index.end:" + DateUtil.formatTimeHHmmss(mPlayBackFragment.getSecondDay(index.getEnd())));
            play(index.getStart());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    protected Bitmap getSaveBitmap() {
        return null;
    }

    public void saveBmp2Gallery() {
        Bitmap bmp = getSaveBitmap();
        if (bmp == null) {
            return;
        }
        int ret = ImageUtils.saveFile(this, bmp);
        if (ret == 0) {
            showToast(R.string.picture_saved_successfully);
        } else {
            showToast(R.string.picture_saved_failed);
        }
    }


    protected void recordStart() {

    }

    public void screenCapClick() {
        String title = getString(com.module.appcommon.R.string.no_permission_to_work_with_screenvideo);
        String content = getString(com.module.appcommon.R.string.enable_permission_storage);
        PermissionUtil permissionUtil = new PermissionUtil(this);
        permissionUtil.checkPermissionFirst(Permission.WRITE_EXTERNAL_STORAGE, this::recordStart, title, content);
    }

    protected List<String> getSpeed() {
        Speed speed = deviceMediaSource.currentSpeed();
        String[] array = getResources().getStringArray(R.array.playback_speeds);
        String current = array[0];
        switch (speed.toValue()) {
            case 1:
                current = array[0];
                break;
            case 2:
                current = array[1];
                break;
            case 4:
                current = array[2];
                break;
        }

        List<String> list = new ArrayList<>();
        list.add(current);
        list.addAll(Arrays.asList(array));
        return list;
    }

    public void screenShotResult() {
        String title =  getString(com.module.appcommon.R.string.no_permission_to_work_with_screenvideo);
        String content =  getString(com.module.appcommon.R.string.enable_permission_storage);
        PermissionUtil permissionUtil = new PermissionUtil(PlaybackActivity.this);
        permissionUtil.checkPermissionFirst(Permission.WRITE_EXTERNAL_STORAGE, this::saveBmp2Gallery, title, content);
    }



    public void speedClick() {
        createSpeedSheetDialogHelper(getSpeed());
    }

    private void createSpeedSheetDialogHelper(List<String> list) {
        CameraBottomSheetDialogHelper.createSpeedSheetDialogHelper(getBottomSheetDialogHelper(), list, (action, text, position) -> {
            switch (action) {
                case BottomSheetListDialog.ACTION_SPEED_X:
                    deviceMediaSource.setSpeed(Speed.x1);
                    mPlayBackFragment.setSpeed(1);
                    deviceFunHelper.setSpeedImageResource(R.drawable.ic_tange_live_speed);
                    break;
                case BottomSheetListDialog.ACTION_SPEED_2X:
                    deviceMediaSource.setSpeed(Speed.x2);
                    mPlayBackFragment.setSpeed(2);
                    deviceFunHelper.setSpeedImageResource(R.drawable.ic_tange_live_speed2);
                    break;
                case BottomSheetListDialog.ACTION_SPEED_4X:
                    deviceMediaSource.setSpeed(Speed.x4);
                    mPlayBackFragment.setSpeed(4);
                    deviceFunHelper.setSpeedImageResource(R.drawable.ic_tange_live_speed4);
                    break;
            }
        });
    }

    protected boolean canClickDeviceFun(){
        if (connectView.getVisibility() == View.VISIBLE){
            String text = connectView.getText().toString();
            if (StringUtils.equalsIgnoreCase(text, getString(R.string.deivce_add_network_error))){
                return false;
            }

            if (StringUtils.equalsIgnoreCase(text, getString(R.string.sim_service_expired))){
                return false;
            }

            if (StringUtils.equalsIgnoreCase(text, getString(R.string.camera_offline))){
                return false;
            }


            if (StringUtils.equalsIgnoreCase(text, getString(R.string.playback_none_sdcard_video))){
                return false;
            }
            if (StringUtils.equalsIgnoreCase(text, getString(R.string.no_video_please_to_scroll))){
                return false;
            }

        }

        return loadingView.getVisibility() != View.VISIBLE;
    }

    protected BottomSheetListDialog getBottomSheetDialogHelper() {
        if (bottomSheetListDialog == null) {
            bottomSheetListDialog = new BottomSheetListDialog(this);
            bottomSheetListDialog.setCanceledOnTouchOutside(true);
            bottomSheetListDialog.setCancelable(true);
        }
        return bottomSheetListDialog;
    }
}
